// pages/course/course.js
const App = getApp()
Page({
  data: {
    courseTypeList: [], //我的课程列表
    courseListData: {}, //课程详情与排课
    currentItem: {},
    professionalScore: {
      name: '专业',
      value: 0,
      type: 'professionalScore'
    }, //专业
    attitudeScore: {
      name: '态度',
      value: 0,
      type: 'attitudeScore'
    }, //态度
    // afterClassScoreAvg: {
    //   name: '课后',
    //   value: 0,
    //   type: 'afterClassScoreAvg'
    // }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      // 设置底部栏选中
      this.getTabBar().setData({
        selected: 1
      })
    }
    this.getCourseList()
  },
  async getCourseList() {
    // 获取课程列表
    let data = await App.$axios.request({
      url: "/parent/myCurriculum/list",
      data: {},
      method: 'GET',
      loading: true,
    })
    if (data && data.length) {
      this.getCourseDetail(data[0])
      this.setData({
        courseTypeList: data,
        currentItem: data[0]
      })
    }
  },
  changeCurrentItem(e) {
    // 头部点击
    let item = e.currentTarget.dataset.item
    if (item.curriculumId && item.curriculumId != this.data.currentItem) {
      this.getCourseDetail(item)
      this.setData({
        currentItem: item
      })
    }
  },
  async getCourseDetail(item) {
    // 获取课程课时信息
    let data
    if (!item.perfectedStudent) {
      this.setData({
        courseListData: {}
      })
      return
    }
    if (item.curriculumType == 2) {
      data = await App.$axios.request({
        url: "/parent/myCurriculum/bigInfo",
        method: 'GET',
        loading: true,
        data: {
          id: item.id,
          curriculumId: item.curriculumId,
          curriculumType: 2,
          studentUserId: item.studentUserId
        },
      })
    } else {
      data = await App.$axios.request({
        url: "/parent/myCurriculum/vipInfo",
        method: 'GET',
        loading: true,
        data: {
          curriculumId: item.curriculumId,
          id: item.id,
          studentUserId: item.studentUserId
        },
      })
    }
    console.log(data)
    if (data) {
      this.setData({
        courseListData: data,
        'professionalScore.value': data.professionalScoreAvg,
        'attitudeScore.value': data.attitudeScoreAvg,
        // 'afterClassScoreAvg.value': data.afterClassScoreAvg
      })
    }
  },
  courseButtonClick(e) {
    let val = e.detail.value
    console.log(val)
    if (val.leaveButtonShow) {
      let that = this
      wx.showModal({
        title: '提示',
        content: '确定要请假吗',
        complete: (res) => {
          if (res.confirm) {
            App.$axios.request({
              url: "/parent/myCurriculum/leave",
              method: 'PUT',
              loading: true,
              data: {
                timeTableDeletedId: val.id,
                studentUserId: that.data.currentItem.studentUserId,
                curriculumType: that.data.currentItem.curriculumType
              },
            }).then(r => {
              that.getCourseList()
            })
          }
        }
      })
    } else if (val.uploadHomeworkButtonShow || val.uploadHomeworkButtonInfoShow) {
      wx.navigateTo({
        url: `/pages/homework/homework?studentUserId=${this.data.currentItem.studentUserId}&type=${this.data.currentItem.curriculumType}&timetableId=${val.id}`,
      })
    }
  },
  bindStudent() {
    let id = this.data.currentItem.id
    let soundCourseType = this.data.currentItem.soundCourseType
    wx.navigateTo({
      url: `/pages/studentList/studentList?bindId=${id}${soundCourseType?'&soundCourseType='+soundCourseType:''}`,
    })
  }
})